<?php

namespace App\Transformers;

use App\Models\StaffPeriodFeeDetail;
use League\Fractal\TransformerAbstract;
use App\Models\StaffPeriodFee;

/**
 * Class StaffPeriodFeeTransformer.
 *
 * @package namespace App\Transformers;
 */
class StaffPeriodFeeTransformer extends TransformerAbstract
{
    /**
     * Transform the StaffPeriodFee entity.
     *
     * @param \App\Models\StaffPeriodFee $model
     *
     * @return array
     */
    public function transform(StaffPeriodFee $model)
    {
        return [
            'id'         => (int) $model->id,
            'company'      => $model->company,
            'staff.name' => $model->staff->name,
            'staffPeriodFeeDetails.dorm.code' => $this->getDormCodes($model),
            'department' => optional($model->staff->department)->name,
            'position' => optional($model->staff->staffPosition)->position,
            'staff.sex' => $model->staff->sex == 1 ? '男' : '女',
            'staff.id_card' => $model->staff->id_card . '&nbsp;',
            'staff.card_num' => $model->staff->card_num,
            'period.name' => $model->period->name,
            'period_status' => $model->period->status,
            'period.start_date' => $model->period->start_date,
            'period.end_date' => $model->period->end_date,
            'dorm_days' => $model->dorm_days,
            'water_fee' => $model->water_fee,
            'hot_water_fee' => $model->hot_water_fee,
            'electric_fee' => $model->electric_fee,
            'manage_fee' => $model->manage_fee,
            'deposit_fee' => $model->deposit_fee,
            'adjust_fee' => $model->adjust_fee,
            'total_fee' => $model->total_fee,
            'adjust_reason' => $model->adjust_reason,
            'created_at' => $model->created_at->format('Y-m-d H:i:s'),
            'updated_at' => $model->updated_at->format('Y-m-d H:i:s')
        ];
    }
    public function getDormCodes($model)
    {
        $staffPeriodFeeDetails = $model->staffPeriodFeeDetails->unique(function ($item) {
            return $item->dorm_id;
        });
        $codes = "";
        foreach ($staffPeriodFeeDetails as $detail) {
            $codes .= " " . $detail->dorm->code;
        }
        return trim($codes);
    }
}
